/*
 * Copyright (C) 2011 mAPPn.Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mol.market.ui_v4;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.mol.market.R;
import com.mol.market.Session;
import com.mol.market.ui.SearchActivity;

/**
 * 我的应用管理页(TabHost)
 * 
 * @author andrew
 * @date 2011-11-4
 * @since Version 0.8.2
 */
public class AppManagerActivity extends SherlockFragmentActivity {

    private Session mSession;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.demo_activity_home);
        mSession = Session.get(this);
        initTabs();
    }

    private void initTabs() {
        ActionBar bar = getSupportActionBar();
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        bar.setDisplayHomeAsUpEnabled(true);
        bar.setHomeButtonEnabled(true);
        bar.setDisplayShowTitleEnabled(true);
        bar.setTitle(R.string.app_management);

        ViewPager pager = (ViewPager) findViewById(R.id.viewpager);

        PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(), this,
                getApplicationContext(), pager, bar);

        ActionBar.Tab tab1 = bar.newTab().setText(R.string.tab_installed);
        ActionBar.Tab tab2 = bar.newTab().setText(R.string.tab_download);
        ActionBar.Tab tab3 = bar.newTab().setText(R.string.tab_download_history);

        adapter.addTab(tab1, AppsUpdateFragment.class, tab1.getText(), null);
        adapter.addTab(tab2, DownloadManagerFragment.class, tab2.getText(), null);
        adapter.addTab(tab3, DownloadHistoryFragment.class, tab3.getText(), null);

        pager.setAdapter(adapter);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getSupportMenuInflater().inflate(R.menu.menu_search, menu);
        return super.onCreateOptionsMenu(menu);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home: {
                onBackPressed();
                return true;
            }
            case R.id.menu_search: {
                Intent i = new Intent(getApplicationContext(), SearchActivity.class);
                startActivity(i);
                return true;
            }
        }
        return super.onOptionsItemSelected(item);
    }

}
